Imports System
Imports System.Text
Imports System.Data
Imports System.Collections.Generic
Imports System.Web

Imports Framework

#Region "Enum: Primary Key Values"


<CLSCompliant(True)> _
Public Enum ESettingType
    Text = 1
    Html = 2
    Image = 3
    [Boolean] = 4
    [Integer] = 5
    ProductId = 6
End Enum
#End Region

'Table-Row Class (Customisable half)
Partial Public Class CSetting

#Region "Constants"
#End Region

#Region "Constructors (Public)"
    'Default Connection String
    Public Sub New()
        MyBase.New()
    End Sub
    
    'Alternative Connection String
    Public Sub New(ByVal dataSrc as CDataSrc)
        MyBase.New(dataSrc)
    End Sub
    
    'Hidden (UI code should use cache instead)
    Protected Friend Sub New([settingId] As Integer)
        MyBase.New([settingId])
    End Sub
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, [settingId] As Integer)
        MyBase.New(dataSrc, [settingId])
    End Sub
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, ByVal [settingId] As Integer, ByVal txOrNull As IDbTransaction)
        MyBase.New(dataSrc, [settingId], txOrNull)
    End Sub
#End Region

#Region "Default Values"
    Protected Overrides Sub InitValues_Custom()
        'm_sampleDateCreated = DateTime.Now
        m_settingSortOrder = 0
    End Sub 
#End Region

#Region "Default Connection String"
    Protected Overrides Function DefaultDataSrc() As CDataSrc
        Return CDataSrc.Default
    End Function
#End Region

#Region "Properties - Relationships"    
    'Relationships - Foriegn Keys (e.g parent)

    'Relationships - Collections (e.g. children)

#End Region

#Region "Properties - Customisation"
    Public Property TypeId As ESettingType
        Get
            Return SettingTypeId
        End Get
        Set(value As ESettingType)
            SettingTypeId = value
        End Set
    End Property
    Public Property SettingBoolean As Boolean
        Get
            If Not SettingBooleanNullable.HasValue Then Return False
            Return SettingBooleanNullable.Value
        End Get
        Set(value As Boolean)
            SettingBooleanNullable = value
        End Set
    End Property
    Public Property SettingBooleanNullable As Boolean?
        Get
            If SettingInteger = Integer.MinValue Then Return Nothing
            Return SettingInteger = 1
        End Get
        Set(value As Boolean?)
            If Not value.HasValue Then
                SettingInteger = Integer.MinValue
            Else
                SettingInteger = IIf(value, 1, 0)
            End If
        End Set
    End Property
    Public ReadOnly Property Value As String
        Get
            Select Case TypeId
                Case ESettingType.Image
                    If String.IsNullOrEmpty(SettingString) Then Return "No Image!"
                    Return SettingString

                Case ESettingType.Text
                    If String.IsNullOrEmpty(SettingString) Then Return "No text!"
                    Return HttpUtility.HtmlEncode(CUtilities.Truncate(SettingString, 60))

                Case ESettingType.Html
                    If String.IsNullOrEmpty(SettingString) Then Return "No html!"
                    Return HttpUtility.HtmlEncode(CUtilities.Truncate(SettingString, 60))

                Case ESettingType.ProductId
                    Dim p As CProduct = CProduct.Cache.GetById(SettingInteger)
                    If IsNothing(p) Then Return "No product selected!"
                    Return p.ProductNameAndContents

                Case ESettingType.Integer
                    If Integer.MinValue = SettingInteger Then Return "No number entered!"
                    Return CTextbox.SetNumber(SettingInteger)

                Case ESettingType.Boolean
                    If Not SettingBooleanNullable.HasValue Then Return String.Empty
                    Return IIf(SettingBoolean, "Yes", "No")
            End Select
            Throw New Exception("Unhandled ESettingType: " & TypeId)
        End Get
    End Property
#End Region

#Region "Save/Delete Overrides"
     'Can Override MyBase.Save/Delete (e.g. Cascade deletes, or insert related records)
#End Region

#Region "Custom Database Queries"
    '(Not normally required for cached classes, use list class for searching etc)
    'For Stored Procs can use: MakeListTyped (matching schema), or DataSrc.ExecuteDataset (reports etc)
    'For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
#End Region

#Region "Searching (Optional)"
    'For cached classes, custom seach logic resides in static methods on the list class
    ' e.g. CSetting.Cache.Search("..")

    'See also the auto-generated methods based on indexes
    ' e.g. CSetting.Cache.GetBy...
#End Region

#Region "Caching Details"
    'Cache Key
    Friend Shared CACHE_KEY As String = GetType(CSetting).ToString 'TABLE_NAME

    'Cache data
    Private Shared Function LoadCache() As CSettingList
        Return New CSetting().SelectAll()
    End Function
    
    'Cache Timeout
    Private Shared Sub SetCache(ByVal value As CSettingList)
        If Not IsNothing(value) Then value.Sort()
        CCache.Set(CACHE_KEY, value) 'Optional parameter can override timeout (otherwise uses config-settings, which default to 3hrs)
    End Sub
    
    'Helper Method
    Private Function CacheGetById(ByVal list As CSettingList) As CSetting
        Return list.GetById(Me.SettingId)
    End Function
#End Region

#Region "Cloning"
    Public Function Clone(ByVal target as CDataSrc, ByVal txOrNull as IDbTransaction) As CSetting ', parentId As Integer) As CSetting
        'Shallow copy: Copies the immediate record, excluding autogenerated Pks
        Dim copy As New CSetting(Me, target)

        'Deep Copy - Child Entities: Cloned children must reference their cloned parent
        'copy.SampleParentId = parentId

        copy.Save(txOrNull)

        'Deep Copy - Parent Entities: Cloned parents also clone their child collections
        'Me.Children.Clone(target, txOrNull, copy.SettingId)

        Return copy
    End Function
#End Region

#Region "ToXml"
    Protected Overrides Sub ToXml_Custom(ByVal w As System.Xml.XmlWriter)
        'Store(w, "Example", Me.Example)
    End Sub
#End Region


End Class
